package comoabejas

class UserController {

	def userService
	
    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [userInstanceList: User.list(params), userInstanceTotal: User.count()]
    }

    def create = {
        def userInstance = new User()
        userInstance.properties = params
        return [userInstance: userInstance]
    }

    def save = {
		
    	def userInstance = new User(params)
		String a = params.password.encodeAsEncrypt()
		
		User u = userService.saveObj(userInstance)
		
        //def userInstance = new User(params)
        if (u.id!=null) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'user.label', default: 'User'), u.id])}"
            redirect(action: "show", id: u.id)
        }
        else {
            render(view: "create", model: [userInstance: u])
        }
    }

    def show = {
        def userInstance = User.get(params.id)
        if (!userInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'user.label', default: 'User'), params.id])}"
            redirect(action: "list")
        }
        else {
            [userInstance: userInstance]
        }
    }

    def edit = {
        def userInstance = User.get(params.id)
        if (!userInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'user.label', default: 'User'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [userInstance: userInstance]
        }
    }

    def update = {
        def userInstance = User.get(params.id)
        if (userInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (userInstance.version > version) {
                    
                    userInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'user.label', default: 'User')] as Object[], "Another user has updated this User while you were editing")
                    render(view: "edit", model: [userInstance: userInstance])
                    return
                }
            }
            userInstance.properties = params
			User u = userService.saveObj(userInstance);
            if (!userInstance.hasErrors() && u!=null) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'user.label', default: 'User'), userInstance.id])}"
                redirect(action: "show", id: userInstance.id)
            }
            else {
                render(view: "edit", model: [userInstance: userInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'user.label', default: 'User'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def userInstance = User.get(params.id)
        if (userInstance) {
            try {
                userInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'user.label', default: 'User'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'user.label', default: 'User'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'user.label', default: 'User'), params.id])}"
            redirect(action: "list")
        }
    }
	
	def generateData = {
		userService.generateData()
		redirect(action: "list")
		
	}
	def login = {
		def u = new User() 
		u.properties = params
		User user = userService.login(u)
		
		session.user = user
		
		if (user){
			redirect(controller:'deal',action:'comprar',params:params)
		}
		else{
			//redirect(controller:'user',action:'loginPage',failedLogin:"true",params:params)
			//chain(action:"loginPage", model:[failedLogin:true,user:params.user,password:params.password])
			//[failedLogin:true,params:params]
			render(view:"login",model:[failedLogin:true,params:params])
			
		}
		
	}
	
	
	def loginPrimerIntento = {
		
		render(view:"login",model:[params:params])
	}
}
